<?php

class Friend extends FaceintegrationBaseModel {

    public $logo;

    public static function getElementById($nid) {
        $node = node_load($nid);
        if ($node !== FALSE)
            return Friend::toModel($node);
        return NULL;
    }

    public static function getElementsByCategory($tid = NULL, $from = 0, $limit = 10000) {
        if ($tid == NULL) {
            $query = new EntityFieldQuery();
            $queryResult = $query->entityCondition('entity_type', 'node')
                    ->propertyCondition('type', 'friend')
                    ->propertyOrderBy('created', 'DESC')
                    ->range($from, $limit)
                    ->execute();
        }
        $result = array();
        if (isset($queryResult['node'])) {
            foreach ($queryResult['node'] as $key => $value) {
                $model = Friend::getElementById($key);
                if ($model != NULL)
                    $result[] = $model;
            }
        }
        return $result;
    }

    public static function toModel($node) {
        $model_friend = new Friend();
        $logo_friend = field_get_items('node', $node, 'field_friend_logo');
        if (count($logo_friend) > 0)
            $model_friend->logo = file_create_url($logo_friend[0]['uri']);
        return $model_friend;
    }

    public static function selectClient() {
        $query = db_select('node_type', 'f')
                        ->condition('f.type', 'friend')
                        ->fields('f', array('description'))
                        ->execute()->fetchField();
        return $query;
    }

}

?>
